<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_grid_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/_ci/cis/cis_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');


class CIs_WP extends TCMS_Admin_Page_W_Grid_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_ci/');

		return $ret;
	}


	public function init() {
		$tvm = $this->get_tvm();
		if (!$tvm->is_page_var_registered('cis', 'last_type')) {
			$tvm->register_page_var('cis', 'last_type');
		}

		parent::init();
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();
		$config = $this->get__config();
		$ret = new CIS_Grid_Ctrl('cis', $tvm, Tangra_Parameter_Method::GET, $config->get_hidden_dir().'inc/');

		$this->export('hidden_inc_dir', $config->get_hidden_dir().'inc/');


		if (!$tvm->is_page_var_registered('ci', 'text')) {
			$tvm->register_page_var('ci', 'text');
			$tmp1 = null;
			$tvm->register_page_var('ci', 'id');
			$tvm->set_page_var('ci', 'id', $tmp1);
		}

		$this->process_filter($tvm);


		return $ret;
	 }


	private function process_filter($vm) {
		$context = $this->get__context();
		if ($context->exists_in_get('text')) {
			$text = $context->get_from_get('text');
			$vm->set_page_var('ci', 'text', $text);
		}

		if ($context->exists_in_get('id')) {
			$id = $context->get_from_get('id');
			if ($id) {
				if (!(tangra_is_int($id) && $id > 0)) {
					$id = '';
				}
				$text = '';
				$vm->set_page_var('ci', 'text', $text);
			}
			$vm->set_page_var('ci', 'id', $id);
		}

		$text = $vm->get_page_var('ci', 'text');
		$id = $vm->get_page_var('ci', 'id');

		$this->export('text', $text);
		$this->export('id', $id);
	 }


	 protected function default_event($page_num = NULL) {
	 	$dbc = $this->get_dbc();

	 	$types = TCMS_CI_Type_DBC::load_all_types($dbc);
	 	$this->export('types', $types);

		$tvm = $this->get_tvm();
	 	$last_type = $tvm->get_page_var('cis', 'last_type');
	 	if (!$last_type) {
	 		$last_type = 0;
	 	}
	 	$this->export('last_type', $last_type);

	 	return parent::default_event($page_num);
	 }
}

